package cn.com.service.impl;

import cn.com.beans.AccountInfo;
import cn.com.beans.UserInfo;
import cn.com.mapper.AccountInfoMapper;
import cn.com.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("mailService")
public class EmailServiceImpl {
    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private JavaMailSender javaMailSender;

    public boolean checkAandE(String phone, String email) {
        System.out.println("checkAandE里面的phone = " + phone);
        List<AccountInfo> accountInfos = accountInfoMapper.selectByPhone(phone);
        List<UserInfo> userInfos = userInfoMapper.selectByEmail(email);

        //ID相同
        Integer aId = accountInfos.get(0).getAId();
        System.out.println("aId = " + aId);
        Integer uAid = userInfos.get(0).getUAid();
        System.out.println("uAid = " + uAid);
        if(aId.intValue() == uAid.intValue()) {
            return true;
        }
        return false;
    }

    public void sendSimpleMail(String to,String number){
        SimpleMailMessage message = new SimpleMailMessage();
        System.out.println("to = " + to);
        System.out.println("from = " + from);
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("验证码");
        message.setText("本次验证码为："+number);
        javaMailSender.send(message);
    }
}